package com.kelvem.saas.workbanch.core.system.dao;

import com.kelvem.saas.workbanch.core.manager.dto.OrgUrlDto;
import com.kelvem.saas.workbanch.fsm.anno.entity.RegisterDao;
import com.kelvem.saas.workbanch.fsm.dao.BaseDao;
import com.kelvem.saas.workbanch.core.system.model.SysUserEntity;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 用户表Dao
 *
 * @author kelvem
 * @version 1.0
 */
@Mapper
@RegisterDao(entityClass= SysUserEntity.class)
@Repository
public interface SysUserDao extends BaseDao<SysUserEntity> {

    // todo 临时放在这
    @Select(" select o.id as org_id, REGEXP_REPLACE(\n" +
            "    REGEXP_REPLACE(pa.url, '<<[^>]*>>', ''),\n" +
            "    '/$', ''\n" +
            "  ) AS url\n" +
            " from sys_org o \n" +
            " inner join sys_org_mapping_menu om on o.id = om.org_id\n" +
            " inner join biz_page p on om.menu_id = p.id\n" +
            " inner join biz_page_action pa on p.id = pa.biz_page_id\n" +
            " where pa.url != ''\n" +
            " \n" +
            " union all\n" +
            " \n" +
            " -- /bizPage/v1/get/10015\n" +
            " select o.id as org_id, concat('/bizPage/v1/get/', p.id) as url\n" +
            " from sys_org o \n" +
            " inner join sys_org_mapping_menu om on o.id = om.org_id\n" +
            " inner join biz_page p on om.menu_id = p.id\n" +
            " where p.page_type = 'meta'\n" +
            " \n" +
            " union all\n" +
            " \n" +
            " -- keyval\n" +
            " select o.id as org_id, \n" +
            "   concat('/graphql/', JSON_UNQUOTE(JSON_EXTRACT(pd.property_json, '$.table')), '/v1/keyValue') as url\n" +
            " from sys_org o \n" +
            " inner join sys_org_mapping_menu om on o.id = om.org_id\n" +
            " inner join biz_page p on om.menu_id = p.id\n" +
            " inner join biz_page_dom pd on p.id = pd.biz_page_id\n" +
            " where pd.control_type = 'select_remote'\n" +
            " \n" +
            " union all\n" +
            " \n" +
            " -- resource\n" +
            " select o.id as org_id, r.resource_url as url\n" +
            " from sys_org o \n" +
            " inner join sys_org_mapping_menu om on o.id = om.org_id\n" +
            " inner join biz_page p on om.menu_id = p.id\n" +
            " inner join sys_resource r on p.id = r.biz_page_id" +
            ";")
    List<OrgUrlDto> queryAllOrgUrlList();
}
